package com.retry;

/**
 * @author zhangyan
 * @version $ Id: RetryDemoTask.java, v 0.1 2021/10/21 16:25 zhangyan Exp $
 */

import cn.hutool.core.date.BetweenFormater;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.format.FastDateFormat;
import lombok.extern.slf4j.Slf4j;
import org.springframework.remoting.RemoteAccessException;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Recover;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Objects;

/**
 * @Author: zgd
 * @Date: 18/11/21 17:01
 * @Description:
 */
@Slf4j
@Service
public class RetryDemoTask {

    /**
     * 重试方法
     */
    @Retryable(value = {Exception.class}, maxAttempts = 15, backoff = @Backoff(delay = 5000L, multiplier = 2))
    public void retry(Integer param) {
        Date date = new Date();
        System.out.println("开始执行了-------------------------"+ DateUtil.format(date,DatePattern.NORM_DATETIME_PATTERN));
        if (Objects.equals(param, 1)) {
            throw new RuntimeException();
        }
    }

    @Recover
    public void recover(Exception e) {
        System.out.println("88888888888888888888888888");
        System.out.println(e.getMessage());
    }


    /**
     * 重试方法
     *
     * @return
     */
    public static boolean retryTask(String param) {
        log.info("收到请求参数:{}", param);
        int i = 10;
        log.info("随机生成的数:{}", i);
        if (i == 0) {
            log.info("为0,抛出参数异常.");
            throw new IllegalArgumentException("参数异常");
        } else if (i == 1) {
            log.info("为1,返回true.");
            return true;
        } else if (i == 2) {
            log.info("为2,返回false.");
            return false;
        } else {
            //为其他
            log.info("大于2,抛出自定义异常.");
            throw new RemoteAccessException("大于2,抛出远程访问异常");
        }
    }
}